package com.cosina.game.crystallight.ui.menu;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Point;
import android.view.KeyEvent;

import com.cosina.game.crystallight.CrystalLightActivity;
import com.cosina.game.crystallight.MediaPlayerAdapter;
import com.cosina.game.crystallight.ui.ResourceManager;
import com.cosina.game.crystallight.ui.ResourceReference;
import com.cosina.game.crystallight.ui.menu.transform.ClickRotateTransform;
import com.cosina.game.crystallight.ui.menu.transform.RotateTransform;
import com.cosina.game.crystallight.ui.menu.transform.TransformSuite;
import com.cosina.game.crystallight.ui.menu.util.ViewPart;
import com.cosina.game.crystallight.util.DialogUtil;

public class MainMenuView extends CompositeView {

	public MainMenuView(CrystalLightActivity activity) {
		super(activity);
		final int height = 56;
		for (int i = 0; i < ResourceReference.IMG_MENU_BUTTONS.length; i++) {
			
			Bitmap bm = ResourceManager.get(ResourceReference.IMG_MENU_BUTTONS[i]);
			
			RotateTransform rt = new RotateTransform();
			rt.rotate = -90 + 30*i;
			TransformSuite ts = TransformSuite.create(rt,new ClickRotateTransform());
			
			Point leftTop = new Point(60,114 + i * height);
			ViewPart buttomInfo = new ViewPart(i, this, bm, leftTop,ts);
			viewParts.add(buttomInfo);
		}
	}

	public int[][] shinesStars = {
			{76-15,29-15},
			{108-15,42-15},
			{112-15,69-15},
			{144-15,40-15},
			{164-15,28-15},
			{196-15,56-15},
			{203-15,36-15},
			{226-15,62-15},
			{234-15,32-15},
			{260-15,20-15}
	};
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		this.invalidate();
	}
	@Override
	public void onButtonClicked(int id) {
		MediaPlayerAdapter.menuBtnclick();
		switch (id) {
		case 0:
			activity.doShowMap();
			return;
		case 1:
			activity.doShowProfileMenu();
			return;
		case 5:
			activity.doShowCreditMenu();
			return;
		}
	}
	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK){
			eventBackUp = event;
			doShowQuiteDialog();
			return true;
		}
		return super.onKeyUp(keyCode, event);
	}
	
	
	private KeyEvent eventBackUp;
	private void doShowQuiteDialog() {
		DialogUtil.show(this.getContext(), "Crystallight", "Quit application?", "YES", "NO", goBackRunnable);
	}
	
	
	private final Runnable goBackRunnable = new Runnable(){
		@Override
		public void run() {
			MainMenuView.super.onKeyUp(KeyEvent.KEYCODE_BACK, eventBackUp);
		}
	};
}
